package cn.ilikes.tools.nosql.jpa.graphs;

import java.util.HashMap;
import java.util.Map;

import cn.ilikes.tools.nosql.jpa.graphs.NodeLifeCycle.LifeCycle;
import cn.ilikes.tools.nosql.jpa.metadatas.EntityMetaData;
import cn.ilikes.tools.nosql.jpa.metadatas.elements.EntityFields;

public class Node {

	public enum State {
		Persist, find, noState, remove
	}

	public static final Node NULL_NODE = null;

	public static final NodeLink NULL_NODE_LINK = null;

	public static EntityMetaData getEntityMetaData(Node node) {
		return node.getEntityMetaData();
	}

	public static Object getEntity(Node node) {
		return node.getEntity();
	}

	public static void switchNodeLifeCycle(Node node, LifeCycle lifeCycle) {
		node.getNodeLifeCycle().setLifeCycle(lifeCycle);
	}

	public static Node createtNode(EntityMetaData entityMetaData, Object entity, LifeCycle lifeCycle) {
		Node node = new Node(entityMetaData, entity, new NodeLifeCycle(lifeCycle));
		return node;
	}

	public class NodeLink {

		private Object targetObject;

		private Object sourceObject;

		public NodeLink() {
			super();

		}

		public Object getTargetObject() {
			return targetObject;
		}

		public Object getSourceObject() {
			return sourceObject;
		}

		public void setTargetObject(Object targetObject) {
			this.targetObject = targetObject;
		}

		public void setSourceObject(Object sourceObject) {
			this.sourceObject = sourceObject;
		}

	}

	////////////////
	private EntityMetaData entityMetaData;

	private Object entity;

	private Map<EntityFields, NodeLink> nodeLink = new HashMap<EntityFields, NodeLink>();

	private NodeLifeCycle nodeLifeCycle;

	private State state = State.noState;

	public Node(EntityMetaData entityMetaData, Object entity, NodeLifeCycle nodeLifeCycle) {
		super();
		this.entityMetaData = entityMetaData;
		this.entity = entity;
		this.nodeLifeCycle = nodeLifeCycle;
	}

	public EntityMetaData getEntityMetaData() {
		return entityMetaData;
	}

	public Object getEntity() {
		return entity;
	}

	@Override
	public String toString() {
		//		return "Node [entityMetaData=" + entityMetaData + ", entity=" + entity + "] parents : "+parents;
		return "Node [entityMetaData=" + entityMetaData + ", entity=" + entity + "] ";
	}

	public Map<EntityFields, NodeLink> getNodeLink() {
		return nodeLink;
	}

	public void setNodeLink(Map<EntityFields, NodeLink> nodeLink) {
		this.nodeLink = nodeLink;
	}

	public NodeLifeCycle getNodeLifeCycle() {
		return nodeLifeCycle;
	}

	public void setNodeLifeCycle(NodeLifeCycle nodeLifeCycle) {
		this.nodeLifeCycle = nodeLifeCycle;
	}

	public State getState() {
		return state;
	}

	public void setState(State state) {
		this.state = state;
	}

}
